home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / allowo1g / listtoht.cls < prev    next >
Encoding:
Visual Basic class definition  |  1999-08-27  |  7.0 KB  |  201 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "ListToHTML"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Attribute VB_Description = "Routines to convert a list to an HTML table"
  15. Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
  16. Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
  17. Option Explicit
  18. ' ListToHTML.cls    July 1999 contact markb@orionstudios.com
  19. ' Encapsulates a method to convert a tab-delimited text file into an HTML Table.
  20. ' Reports progress by raising an Event.
  21. ' Requires Project/References entry for
  22. '   Microsoft HTML Object Library (MSHTML.tlb)
  23. '
  24. ' NOTE: The purpose is to demonstrate DOM manipulation. The Tabular Data Control
  25. '       (TDC.ocx) is a good solution for displaying delimited text files.
  26. '=================================================================================
  27. ' Events
  28. Public Event RowProgress(RowNum As Long)                ' Once at start of processing
  29. Public Event RowsCols(NumRows As Long, NumCols As Long) ' Frequency = ProgressInterval
  30. 'Module-level variables to hold property values
  31. Private mvarListCaption As String       ' no default
  32. Private mvarProgressInterval As Long    ' default = 5
  33. Private mvarCancel As Boolean           ' allows interruption of main processing loop
  34. ' Module-level variables
  35. Private mFootText As String
  36.  
  37. Public Property Let ListCaption(ByVal vData As String)  ' Optional
  38.     mvarListCaption = vData
  39. End Property
  40.  
  41. Public Property Let ProgressInterval(ByVal vData As Long)   ' Default = 5
  42.     mvarProgressInterval = vData
  43. End Property
  44.  
  45. Public Property Let Cancel(ByVal vData As Boolean)
  46.     mvarCancel = vData
  47.     mFootText = "PROCESS CANCELLED - list may be incomplete"
  48. End Property
  49.  
  50. Public Function FileToDOM( _
  51.             InFileName As String, _
  52.             HTMLDoc As MSHTML.HTMLDocument, _
  53.             Optional GetTotalRows As Boolean = False) As MSHTML.HTMLTable
  54.             
  55.     On Error GoTo FileToDOM_Error
  56.  
  57.     Const sngZero As Single = 0
  58.     
  59.     Dim Result As MSHTML.HTMLTable
  60.     Dim oTable As MSHTML.HTMLTable
  61.     Dim oTBody As MSHTML.HTMLTableSection
  62.     Dim oRow As MSHTML.HTMLTableRow
  63.     Dim oRowClone As MSHTML.HTMLTableRow
  64.     Dim oCol As MSHTML.HTMLTableCol
  65.     Dim oCell As MSHTML.HTMLTableCell
  66.     Dim IsNumCol() As Boolean   ' attempt to speed numeric test in BODY loop
  67.     Dim rowIX As Long, rowIXmax As Long
  68.     Dim colIX As Long, colIXmax As Long
  69.     Dim InFile As Scripting.TextStream
  70.     Dim varCols As Variant
  71.     Dim strCell As String
  72.  
  73. ' Open input file
  74.     With New Scripting.FileSystemObject
  75.     
  76.     ' Read ahead to second line (first line of data)
  77.         With .OpenTextFile( _
  78.                 FileName:=InFileName, _
  79.                 IOMode:=ForReading)
  80.             .ReadLine   ' skip headings line
  81.             varCols = Split(.ReadLine, vbTab)   ' first row of data
  82.             If GetTotalRows Then ' If requested, pre-read file to get total rows
  83.                 rowIXmax = 2
  84.                 Do Until .AtEndOfStream
  85.                     .ReadLine
  86.                     rowIXmax = rowIXmax + 1
  87.                 Loop
  88.             End If
  89.             .Close
  90.         End With
  91.     
  92.     ' Re-open file for main loop
  93.         Set InFile = .OpenTextFile( _
  94.                 FileName:=InFileName, _
  95.                 IOMode:=ForReading)
  96.     End With
  97.     
  98. ' Keep range dimensions in local variables (see above for rowIXmax; may be zero)
  99.     colIXmax = UBound(varCols)
  100.     RaiseEvent RowsCols(rowIXmax, colIXmax + 1)
  101.  
  102. ' Create row for cloning
  103.     With HTMLDoc
  104.         Set oRow = .createElement("TR")
  105.         For colIX = 0 To colIXmax
  106.             Set oCell = oRow.appendChild(.createElement("TD"))
  107.             oCell.appendChild .createTextNode(" ")
  108.         Next
  109.     End With
  110.     
  111. ' Create the Table Object
  112.     Set oTable = HTMLDoc.createElement(etag:="TABLE")
  113.     oTable.id = "idTable"
  114.     
  115. ' Column specifications (Not foolproof; blank field in first row always non-numeric)
  116.     ReDim IsNumCol(0 To colIXmax)
  117.     For colIX = 0 To colIXmax   ' in first row of data (line 2)
  118.         Set oCol = oTable.appendChild(HTMLDoc.createElement("COL"))
  119.         IsNumCol(colIX) = IsNumeric(varCols(colIX))
  120.         oCol.className = IIf(IsNumCol(colIX), "clNum", "clText") ' Does not render ??
  121.     Next colIX
  122.  
  123. ' Body
  124.     Set oTBody = oTable.appendChild(HTMLDoc.createElement("TBODY"))
  125.     Do Until InFile.AtEndOfStream
  126.         varCols = Split(InFile.ReadLine, vbTab)
  127.         rowIX = rowIX + 1
  128.         Set oRowClone = oTBody.appendChild(oRow.cloneNode(fdeep:=True))
  129.         For colIX = 0 To colIXmax
  130.             strCell = varCols(colIX)
  131.             Set oCell = oRowClone.childNodes(colIX)
  132.             With oCell
  133.                 .firstChild.nodeValue = strCell
  134.                 If IsNumCol(colIX) Then
  135.                     .runtimeStyle.TextAlign = "right"   ' clNum Does not render ??
  136.                     If IsNumeric(strCell) Then  ' beter than Len(strCell) > 0
  137.                         If CSng(strCell) < sngZero Then
  138.                             .className = "clNumNeg"
  139.                             .runtimeStyle.Color = "red" ' clNumNeg Does not render ??
  140.                         End If
  141.                     End If
  142.                 End If
  143.             End With
  144.         Next colIX
  145.         If rowIX Mod mvarProgressInterval = 0 Then
  146.             RaiseEvent RowProgress(rowIX)
  147.             If mvarCancel Then
  148.                 Beep
  149.                 Exit Do
  150.             End If
  151.         End If
  152.     Loop    ' on Infile
  153.     InFile.Close
  154.  
  155. ' Header Row: use first row as column headings (i.e., move from TBODY to THEAD)
  156.     With oTable.createTHead
  157.         .appendChild oTBody.firstChild
  158.     End With
  159.     
  160. ' Footer Row
  161.     With oTable.createTFoot
  162.         With .appendChild(HTMLDoc.createElement("TR"))
  163.             With .appendChild(HTMLDoc.createElement("TD"))
  164.                 .colSpan = colIXmax + 1
  165.                 .appendChild HTMLDoc.createTextNode(mFootText)
  166.                 If mvarCancel Then
  167.                 .runtimeStyle.backgroundColor = "red"
  168.                 End If
  169.             End With
  170.         End With
  171.     End With
  172.  
  173. ' Caption (if ListCaption Property is specified)
  174.     If Len(mvarListCaption) Then
  175.         With oTable.createCaption
  176.             .appendChild HTMLDoc.createTextNode(mvarListCaption)
  177.             .runtimeStyle.display = "inline"
  178.         End With
  179.     End If
  180.     
  181.     RaiseEvent RowProgress(rowIX)
  182.     Set Result = oTable
  183.     
  184. FileToDOM_Exit:
  185.     Set FileToDOM = Result
  186.     Exit Function
  187.  
  188. FileToDOM_Error:
  189.     MsgBox Err.Number & " - " & Err.Description, vbExclamation, "FileToDOM"
  190.     Resume FileToDOM_Exit
  191.     
  192. End Function
  193.  
  194. Private Sub Class_Initialize()
  195.  
  196.     mvarProgressInterval = 5 ' default ProgressInterval
  197.     mFootText = "End of List"   ' Changed if conversion cancelled
  198.     
  199. End Sub
  200.  
  201.